package com.example.scribe.backend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Minimal local variable allocator for parameters (locals to be added later).
 */
public final class LocalVariableAllocator {
    private final Map<String, Integer> nameToIndex = new HashMap<>();
    private int nextLocalIndex;

    public LocalVariableAllocator(List<String> parameterNames) {
        int index = 0;
        for (String param : parameterNames) {
            nameToIndex.put(param, index++);
        }
        this.nextLocalIndex = index;
    }

    public int getIndexByName(String name) {
        Integer idx = nameToIndex.get(name);
        if (idx == null) throw new IllegalStateException("Unknown variable: " + name);
        return idx;
    }

    public boolean hasName(String name) {
        return nameToIndex.containsKey(name);
    }

    public int allocateLocal(String name) {
        int idx = nextLocalIndex++;
        nameToIndex.put(name, idx);
        return idx;
    }

    public int getMaxLocals(int paramCount) {
        return Math.max(paramCount, nextLocalIndex);
    }
}


